package cn.kilo.domain.service.order.impl;

import cn.kilo.domain.model.order.entity.OrderEntity;
import cn.kilo.domain.model.order.entity.StaffEntity;
import cn.kilo.domain.model.order.vo.OrderStatusVO;
import cn.kilo.domain.model.order.res.DepartmentRes;
import cn.kilo.domain.service.order.AbstractStaffStrategyChain;
import cn.kilo.domain.service.order.factory.DefaultFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author kilok
 */
@Slf4j
@Component("user")
public class UserStrategy extends AbstractStaffStrategyChain {
    /**
     * 用户下订单
     *
     * @param order
     * @return
     */
    @Override
    public List<OrderStatusVO> doJob(OrderEntity order) {
        log.info("用户下单:{}", order);
        String userName = repository.queryUserNameById(order.getUserId());

        order.setOrderId(snowFlake.nextId());
        order.setStatus(DefaultFactory.Status.CREATE.getInfo());

        DepartmentRes departmentRes = repository.getDepartmentAndFloor(order.getRidgepole());
        log.info("公寓信息:{}",departmentRes);
        List<OrderStatusVO> list=new ArrayList<>();
        if (null == departmentRes) {
            list.add( build(null, userName, DefaultFactory.Status.WRONG_POSITION.getInfo(), order.getStart(), order.getEnd(), order.getDistance(), order.getRidgepole(), LocalDateTime.now(), order.getOrderInfo(), order.getType(), order.getOrderCode(), order.getOrderNumber(),order.getPrice(),order.getUserId()));
            return list;
        }
        Integer distance = repository.judgeRoute(order.getStart(), departmentRes.getDepartmentGroup());
        if (!order.checkFloor(departmentRes.getFloor())) {
            list.add(build(null, userName, DefaultFactory.Status.WRONG_FLOOR.getInfo(), order.getStart(), order.getEnd(), order.getDistance(), order.getRidgepole(), LocalDateTime.now(), order.getOrderInfo(), order.getType(),order.getOrderCode(),order.getOrderNumber(),order.getPrice(),order.getUserId()));
            return list;
        }
        if (-1 == distance) {
//            list.add(build(null, userName, DefaultFactory.Status.WRONG_POSITION.getInfo(), order.getStart(), order.getEnd(), order.getDistance(), order.getRidgepole(), LocalDateTime.now(), order.getOrderInfo(), order.getType(),order.getOrderCode(), order.getOrderNumber(), order.getPrice(),order.getUserId()));
//            return list;
            distance=100;
        }
        order.setDistance(distance);
        boolean res = repository.saveOrder(
                build(order.getOrderId(),
                        userName, DefaultFactory.Status.CREATE.getInfo(),
                        order.getStart(),
                        order.getEnd(),
                        order.getDistance(),
                        order.getRidgepole(),
                        LocalDateTime.now(),
                        order.getOrderInfo(),
                        order.getType(),
                        order.getOrderCode(),
                        order.getOrderNumber(),
                        order.getPrice(),order.getUserId()),
                order.getUserId());
        if (res) {
            list.add(build(order.getOrderId(), userName, DefaultFactory.Status.CREATE.getInfo(), order.getStart(), order.getEnd(), order.getDistance(), order.getRidgepole(), LocalDateTime.now(), order.getOrderInfo(), order.getType(),order.getOrderCode(),order.getOrderNumber(),order.getPrice(),order.getUserId()));
            return list;
        } else {
            list.add(build(null, userName, DefaultFactory.Status.FAIL.getInfo(), order.getStart(), order.getEnd(), order.getDistance(), order.getRidgepole(), LocalDateTime.now(), order.getOrderInfo(), order.getType(),order.getOrderCode(),order.getOrderNumber(),order.getPrice(),order.getUserId()));
            return list;
        }
    }

    @Override
    public DefaultFactory.Role getRole() {
        return DefaultFactory.Role.USER;
    }

    @Override
    public Map<String, Integer> getLeftStaff(OrderEntity order) {
        return null;
    }

    @Override
    public boolean beStaff(StaffEntity staffEntity) {
        return false;
    }
}
